package com.sky.aspect;

import com.sky.annotation.autoFile;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import  org.aspectj.lang.reflect.MethodSignature;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Aspect
@Component
/**
 * 自定义切面，实现公共字段自动填充处理逻辑
 */
public class autoFileAspect {

    /**
     * 切入点
     */

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.autoFile)")
    public void autoFilePointCut(){}



    @Before("autoFilePointCut()")
    public void autoFile(JoinPoint joinPoint){
        log.info("公共字段自动填充处理逻辑");
        //获取当前被拦截的方法上的注解的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获取方法签名
        autoFile annotation = signature.getMethod().getAnnotation(autoFile.class);//获取方法上的注解对象
        OperationType operationType = annotation.value();
        //获取方法上的参数-实体对象
        Object[] args = joinPoint.getArgs();
        if(args==null||args.length==0){
            return;
        }
        Object arg = args[0];

        // 定义一个格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //准备赋值的数据
        LocalDateTime now=LocalDateTime.now();
        Long CurrentId = BaseContext.getCurrentId();
        log.info("拦截的id{}",CurrentId);
        //根据不同的操作类型，通过反射进行复制
        if(operationType==OperationType.INSERT){//为四个字段进行赋值
            try {
                Method setCreateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setCreateUser = arg.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreateTime.invoke(arg,now);
                setUpdateTime.invoke(arg,now);
                setCreateUser.invoke(arg,CurrentId);
                setUpdateUser.invoke(arg,CurrentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        } else if (operationType==OperationType.UPDATE) {
            try {
                Method setUpdateTime = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = arg.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(arg,now);
                setUpdateUser.invoke(arg,CurrentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }


    }


}
